package com.bing.services;

import com.bing.bean.sys.SysUser;
import com.bing.bean.sys.SysUserStatus;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Administrator on 2015/6/13.
 */

@Service
public class BingouRealm extends AuthorizingRealm {
    @Autowired
    SysUserService sysUserService;

    private static final Logger logger = LoggerFactory.getLogger(BingouRealm.class);

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }


    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String username = (String) token.getPrincipal();
        SysUser sysUser1 = sysUserService.findByName("bingou");

        SysUser sysUser = sysUserService.findByName(username);
        if (sysUser == null) {
            throw new UnknownAccountException("此帐号没有注册");
        }
        if (sysUser.getStatus() != SysUserStatus.active) {
            throw new DisabledAccountException("此帐号未激活");
        }
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(sysUser.getName(), sysUser.getPassword(), getName());

        return info;
    }
}
